<?php
/**
 * @ClassName: AmapLogic
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-20 13:41
 * @Version: V1.0.0
 */

namespace app\common\logic;

class AmapLogic
{
    /**
     * 地理编码
     * @param $address
     * @return array
     */
    static function geo($address)
    {
        $data = [];

        if (!$address) {
            return ['code' => 0, 'msg' => 'OK', 'data' => $data];
        }

        $res = amap()->geo($address);
        if (intval($res['count']) > 0) {
            $geocodes = $res['geocodes'][0];
            $data['province'] = $geocodes['province'];
            $data['city'] = $geocodes['city'];
            $data['area'] = $geocodes['district'];
            $data['street'] = $geocodes['street'];
            $data['areaStr'] = $geocodes['province'] . $geocodes['city'] . $geocodes['district'];
            $data['areaCode'] = $geocodes['adcode'];
            $data['address'] = $geocodes['formatted_address'];

            $location = explode(',', $geocodes['location']);
            $data['longitude'] = $location[0];
            $data['latitude'] = $location[1];

            return ['code' => 1, 'msg' => 'OK', 'data' => $data];
        }

        return ['code' => 0, 'msg' => 'OK', 'data' => $data];
    }

}